package com.plian.system.controller.sys.user;

import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.service.sys.orgzation.OrgzationService;
import com.plian.system.service.sys.user.IWfGroupRelationService;
import com.plian.system.service.sys.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;

@RestController
@RequestMapping(value = "api/user")
public class userController extends BaseController {
    @Autowired
    UserService userService;

    @Autowired
    OrgzationService orgzationService;

    @Autowired
    IWfGroupRelationService wfGroupRelationService;

    @PostMapping(value = "/save")
    @LogOperation("用户保存")
    public ObjResult save(@RequestBody PageData pd) {
        //参数userName，passWord
        return userService.save(pd);
    }

    @PostMapping(value = "/edit")
    @LogOperation("用户编辑")
    public ObjResult edit(@RequestBody PageData pd) {
        //参数userName，passWord
        return userService.edit(pd);
    }
    @PostMapping(value = "/saveGroup")
    public ObjResult saveGroup(@RequestBody PageData pd) {
        return userService.saveGroup(pd);
    }
    @PostMapping(value = "/editGroup")
    public ObjResult editGroup(@RequestBody PageData pd) {
        return userService.editGroup(pd);
    }
    @PostMapping(value = "/getGroupPer")
    public ObjResult getGroupPer(@RequestBody PageData pd) {
        return userService.getGroupPer(pd);
    }
    @PostMapping(value = "/listByCreatUser")
    public ListResult listByCreatUser(@RequestBody PageData pd) {
        return userService.listByCreatUser(pd);
    }

    @PostMapping(value = "/findid")
    public ObjResult findid(@RequestBody PageData pd) {
        return userService.findbyid(pd);
    }

    @PostMapping(value = "/findByIds")
    public ListResult<PageData> findByIds (@RequestBody PageData pd){
        return userService.findByIds((List<String>)pd.get("ids"));
    }

    @PostMapping(value = "/del_ids")
    public ObjResult delids(@RequestBody PageData pd) {
        return userService.delids((List<String>) pd.get("ids"));
    }

    @PostMapping(value = "/listpage")
//    @PassToken
    public ListResult<PageData> listpage(@RequestBody PageData pd) {
        return userService.listpage(pd);
    }

    @PostMapping(value = "/list")
//    @PassToken
    public ListResult<PageData> list() {
        return userService.listpage(this.getPageData());
    }

    /**
     * 根据单位id获取单位下的人员
     * @param pd 请求数据
     */
    @PostMapping("select-by-organization-id")
    public ListResult<PageData> selectByOrganizationId(@RequestBody PageData pd) {
        PageData pageData = new PageData();
        pageData.put("id",pd.getString("organizationId"));
        PageData organization = orgzationService.findbyid(pageData).getPageData();
        if (organization.getInteger("type")==4){
            return userService.getUsersUnderGZW();
        }else {
            return userService.selectByOrganizationId(pd);
        }
    }
    @PostMapping(value = "/userEdit")
    public ObjResult userEdit(@RequestBody PageData pd){
        return userService.userEdit(pd);
    }

    @PostMapping(value = "/listForLowerUnit")
    public ListResult<PageData> listForLowerUnit() {
        return userService.listForLowerUnit(new PageData());
    }

    @GetMapping(value="/unlock/{id}")
    public ObjResult unlock(@PathVariable String id){
        return userService.unlock(id);
    }

    @PostMapping(value = "/upLoadExlce")
    public ObjResult upLoadExlce(MultipartHttpServletRequest mp){
        return userService.upLoadExlce(mp);
    }

    @GetMapping(value = "/initGroup")
//    @PassToken
    public ObjResult initGroup(){
        wfGroupRelationService.init();
        return new ObjResult(CommonCode.SUCCESS);
    }

    @GetMapping(value = "/createFirstGroup")
//    @PassToken
    public ObjResult createFirstGroup(){
        wfGroupRelationService.createFirstGroup();
        return new ObjResult(CommonCode.SUCCESS);
    }

    @GetMapping("/export")
    public ObjResult export(){
        return userService.exportImportFile();
    }

    @GetMapping("/encryptBase64Data")
    public void encryptBase64Data(){
        userService.encryptBase64Data();
    }


    /**
     * 用户状态修改
     * @param pd
     * @return
     */
    @PostMapping(value = "/editStatus")
    public ObjResult editStatus(@RequestBody PageData pd) {
        return userService.editStatus(pd);
    }

}
